class RateCLI
  module Create

    def create(target)
      puts info("Creating #{target}")
      eval("create_#{target}")
    end

    def create_view
      begin
        loop do
          name = @sui.ask "name:"
          strategy = choose_list "strategy:", %w{file import_tag all}
          path, import_tag = nil
          if strategy == 'file'
            path = ask_file "path:"
          elsif strategy == 'import_tag'
            import_tag = @sui.ask "import_tag:"
          end
          desc = @sui.ask "description"

          options = { name: name, strategy: strategy, path: path,
                    import_tag: import_tag, desc: desc }

          table = hash2table(options)
          puts table
          yes = @sui.ask_yes_no "Sure?"
          
          if yes
            result = @client.create_view(options)
            if result.success?
              success "view #{name} created"
              success "num of classes: #{result.class_count}"
              success "num of samples: #{result.sample_count}"
              break
            else
              error "can't create view: #{result.message}"
              yes = @sui.ask_yes_no "Try again?"
              break if not yes
            end
          end
        end
      rescue Interrupt => e
        puts e.to_s
      end
    end

    def create_user
      begin
        loop do
          params = {}
          params[:name] = @sui.ask "name:"
          params[:email] = @sui.ask "email:"
          params[:password] = @sui.ask_for_password "password:"
          params[:org] = @sui.ask "organization:"

          print_options(params)
          yes = @sui.ask_yes_no "Sure?"

          if yes
            result = @client.create_user params
            if result.success?
              success "user #{params[:name]} created"
              break
            else
              error "Can't creat user: #{result.message}"
              error "Try use another email"
              yes = @sui.ask_yes_no "Try again?"
              break if not yes
            end
          end
        end
      rescue Interrupt => e
        puts ""
      end
    end

    def create_benchmark
      views = @client.list(target: 'view')

      if views.contents.empty?
        info "No views yet, create one before creating benchmark"
        return
      end

      begin
        loop do
          options = {}
          options[:name] = @sui.ask("name:")
          i = choose_from_harray("Choose a view", views.contents, %w{name uuid created})
          options[:view] = views.contents[i-1]
          options[:strategy] = choose_list "strategy:", %w{general file all allinner allinter allInnerOneInter}
          if options[:strategy] == 'general'
            options[:class_count] = @sui.ask("class_count:")
            options[:sample_count] = @sui.ask("sample_count:")
          elsif options[:strategy] == 'file'
            options[:path] = ask_file "path:"
          end

          options[:desc] = @sui.ask("description:")

          print_options(options)
          yes = @sui.ask_yes_no("Sure?")

          if yes
            result = @client.create_benchmark options
            if result.success?
              success "benchmark #{options[:name]} created"
              success "#{result['description']}"
              break
            else
              error "Can't creat benchmark: #{result.message}"
              yes = @sui.ask_yes_no "Try again?"
              break if not yes
            end
          end
        end
      rescue Interrupt => e
      end
    end

    def create_algorithm
      begin
        loop do
          options = {}
          options[:name] = @sui.ask "name:"
          options[:tag] = @sui.ask "tag:"
          options[:path] = ask_file "path (must be zip):"        

          print_options options
          yes = @sui.ask_yes_no("Sure?")

          if yes
            result = @client.create_algorithm options
            if result.success?
              success "algorithm #{options[:name]} created"
              break
            else
              error "Can't creat algorithm: #{result.message}"
              yes = @sui.ask_yes_no "Try again?"
              break if not yes
            end
          end
        end
      rescue Interrupt => e
        puts
      end
    end
  end
end